<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <base href="<%=request.getContextPath()%>/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册 - 医疗预约系统</title>
    <link rel="stylesheet" href="statics/css/hisStyle.css">
    <style>
        body {
            background-color: #f5f7fa;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .register-container {
            max-width: 500px;
            margin: 50px auto;
            padding: 40px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .register-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .register-header h1 {
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 28px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: bold;
            font-size: 14px;
        }
        .form-control {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            box-sizing: border-box;
            transition: border-color 0.3s, box-shadow 0.3s;
        }
        .form-control:focus {
            outline: none;
            border-color: #3498db;
            box-shadow: 0 0 5px rgba(52, 152, 219, 0.3);
        }
        .btn-register {
            width: 100%;
            padding: 12px;
            background-color: #27ae60;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .btn-register:hover {
            background-color: #229954;
        }
        .login-link {
            text-align: center;
            margin-top: 20px;
        }
        .login-link a {
            color: #3498db;
            text-decoration: none;
            transition: color 0.3s;
        }
        .login-link a:hover {
            color: #2980b9;
            text-decoration: underline;
        }
        .error-message {
            color: #e74c3c;
            background-color: #fdf2f2;
            border: 1px solid #fecaca;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        .form-row {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
        }
        .form-row .form-group {
            flex: 1;
            min-width: 200px;
        }
        .required-field {
            color: #e74c3c;
        }
        @media (max-width: 768px) {
            .register-container {
                margin: 30px 20px;
                padding: 30px;
            }
            .form-row {
                flex-direction: column;
            }
            .form-row .form-group {
                min-width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="register-header">
            <h1>用户注册</h1>
            <p>创建您的医疗预约账号</p>
        </div>

        <!-- 错误提示 -->
        <c:if test="${not empty error}">
            <div class="error-message">
                ${error}
            </div>
        </c:if>

        <form action="auth/register" method="post" id="registerForm">
            <div class="form-row">
                <div class="form-group">
                    <label for="username">用户名：<span class="required-field">*</span></label>
                    <input type="text" id="username" name="username" class="form-control" required minlength="2" maxlength="20" placeholder="请输入用户名">
                </div>
                <div class="form-group">
                    <label for="phone">手机号：<span class="required-field">*</span></label>
                    <input type="tel" id="phone" name="phone" class="form-control" required pattern="1[3-9]\d{9}" placeholder="请输入手机号码">
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="password">密码：<span class="required-field">*</span></label>
                    <input type="password" id="password" name="password" class="form-control" required minlength="6" maxlength="20" placeholder="请输入密码（至少6位）">
                    <small class="form-text text-muted">密码必须包含字母和数字，长度6-20位</small>
                </div>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label for="confirmPassword">确认密码：<span class="required-field">*</span></label>
                    <input type="password" id="confirmPassword" name="confirmPassword" class="form-control" required placeholder="请再次输入密码">
                    <div id="passwordMatchHint" class="text-danger" style="display:none;">两次输入的密码不一致</div>
                </div>
            </div>
            
            <div class="form-group">
                <label for="email">邮箱：</label>
                <input type="email" id="email" name="email" class="form-control" placeholder="请输入邮箱（选填）">
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="realName">真实姓名：</label>
                    <input type="text" id="realName" name="realName" class="form-control" placeholder="请输入真实姓名（选填）">
                </div>
                <div class="form-group">
                    <label for="sex">性别：</label>
                    <select id="sex" name="sex" class="form-control">
                        <option value="0">未设置</option>
                        <option value="1">男</option>
                        <option value="2">女</option>
                    </select>
                </div>
            </div>
            
            <div class="form-group">
                <label for="birthday">出生日期：</label>
                <input type="date" id="birthday" name="birthday" class="form-control">
            </div>

            <button type="submit" class="btn-register">注册</button>
        </form>

        <script>
            // 表单验证
            document.getElementById('registerForm').addEventListener('submit', function(e) {
                const username = document.getElementById('username').value;
                const phone = document.getElementById('phone').value;
                const password = document.getElementById('password').value;
                const confirmPassword = document.getElementById('confirmPassword').value;
                const email = document.getElementById('email').value;
                
                // 用户名验证
                if (username.length < 2 || username.length > 20) {
                    alert('用户名长度应在2-20个字符之间');
                    e.preventDefault();
                    return;
                }
                
                // 手机号验证
                const phoneRegex = /^1[3-9]\d{9}$/;
                if (!phoneRegex.test(phone)) {
                    alert('请输入有效的手机号码');
                    e.preventDefault();
                    return;
                }
                
                // 密码验证
                if (password.length < 6 || password.length > 20) {
                    alert('密码长度应在6-20个字符之间');
                    e.preventDefault();
                    return;
                }
                
                // 实时验证密码匹配
                document.getElementById('confirmPassword').addEventListener('input', function() {
                    const password = document.getElementById('password').value;
                    const confirmPassword = this.value;
                    const hint = document.getElementById('passwordMatchHint');
                    
                    if (confirmPassword && password !== confirmPassword) {
                        hint.style.display = 'block';
                    } else {
                        hint.style.display = 'none';
                    }
                });
                
                // 确认密码验证
                if (password !== confirmPassword) {
                    alert('两次输入的密码不一致');
                    e.preventDefault();
                    return;
                }
                
                // 密码强度验证
                const hasLetter = /[a-zA-Z]/.test(password);
                const hasNumber = /[0-9]/.test(password);
                if (!hasLetter || !hasNumber) {
                    alert('密码必须包含字母和数字');
                    e.preventDefault();
                    return;
                }
                
                // 邮箱验证（如果填写了）
                if (email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
                    alert('请输入有效的邮箱地址');
                    e.preventDefault();
                    return;
                }
            });
        </script>

        <div class="login-link">
            <p>已有账号？<a href="auth/login">立即登录</a></p>
        </div>
    </div>
</body>
</html>
